package com.elead.oa.controller;

import com.elead.oa.entity.Employee;
import com.elead.oa.service.employee.IEmployeeService;
import com.elead.oa.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author xiaozuolin
 * @date 2021/10/8 8:55
 */
@Controller
@CrossOrigin
@Slf4j
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 登录页面
     *
     * @param
     * @return
     */
    @GetMapping("/login")
    public String login() {

        return "login";
    }

    @PostMapping("/logined")
    @ResponseBody
    public Employee logined(@RequestBody LoginVo loginVo, HttpSession session) {
        if (StringUtils.isEmpty(loginVo.getUsername())) {
            throw new RuntimeException("请输入账号");
        }
        if (StringUtils.isEmpty(loginVo.getPassword())) {
            throw new RuntimeException("请输入密码");
        }
        Employee employee = employeeService.logined(loginVo);
        if (employee == null) {
            throw new RuntimeException("账号或密码错误");
        }
        if (!employee.getPassword().equals(loginVo.getPassword())) {
            throw new RuntimeException("账号或密码错误");
        }
        session.setAttribute("sessionEmployee", employee);
        return employee;
    }

}
